package com.dhee.creditcardsystem.controller;

import com.dhee.creditcardsystem.model.App;
import com.dhee.creditcardsystem.model.Users;
import com.dhee.creditcardsystem.service.AppService;
import com.dhee.creditcardsystem.service.UsersService;
import com.dhee.creditcardsystem.util.DBUtilAppChange;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@SessionAttributes({"app"})
@RequestMapping("apply")
public class AppController {

    @Autowired
    private AppService appService;
    @Autowired
    private UsersService usersService;

    @RequestMapping("toregister")
    public String toregister() {

        return "register";
    }

    @RequestMapping("register")
    public String register(Users users, Model model, HttpSession session) {



        String currentTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());

        users.setInsertdte(currentTime);
        users.setInsertid(users.getLoginn());
        users.setUpdatedte(currentTime);
        users.setUpdateid(users.getLoginn());
        users.setDeleteflg("0");     // 逻辑删除字段 0 表示 不删除 1 表示 删除
        users.setDeletedte(currentTime);


        if (usersService.addUsers(users) != 0) {




            return "login";
        }
        return "error";
    }



    @RequestMapping("toqueren")
    public String toqueren(App app, Model model, HttpSession session) throws SQLException {
        return "apply/A1A01WA01A02 入会申込情報入力";
    }

    @RequestMapping("queren")
    public String queren(App app, Model model, HttpSession session) throws SQLException {

        return "apply/A1A01WA01A03 入会申込情報入力";
    }
    @RequestMapping("querena")
    public String querena(App app, Model model, HttpSession session) throws SQLException {

        return "apply/a";
    }
    @RequestMapping("a")
    public String a(@Valid App app, BindingResult bindingResult, Model model, HttpSession session) throws SQLException {

        // 检查是否有验证错误
        if (bindingResult.hasErrors()) {
            model.addAttribute("app", app);
            return "apply/a"; // 返回表单页面显示错误
        }

        String cstid = (String) session.getAttribute("cstid");
        app.setCstid(cstid);

        // 设置日期字段为当前时间
        String currentTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        app.setInsertdte(currentTime);
        app.setUpdatedte(currentTime);
        System.out.println(app.getInsertdte());
        System.out.println(app.getUpdatedte());


        app.setSyokbn("10");
        app.setAppsts("01");

        App app1 = appService.selectByCstId(cstid);
        if (app1 != null) {
            app1 = app;
            appService.updateApp(app1);
            return "apply/A1A01WA01A06入会申込情報入力";
        } else if (appService.addApp(app)) {

            return "apply/A1A01WA01A06入会申込情報入力";

        }
//        if (app.getCstid() != null) {
//            appService.updateApp(app);
//            return "apply/A1A01WA01A04 入会申込情報入力";
//        } else


        return "error";
    }

    @RequestMapping("xinxi1")
    public String xinxi1(@Valid App app, BindingResult bindingResult, Model model, HttpSession session) throws SQLException {


        // 检查是否有验证错误
        if (bindingResult.hasErrors()) {
            model.addAttribute("app", app);
            bindingResult.getAllErrors().forEach(System.out::println);
            return "apply/A1A01WA01A03 入会申込情報入力"; // 返回表单页面显示错误
        }





        String cstid = (String) session.getAttribute("cstid");
        app.setCstid(cstid);

        // 设置日期字段为当前时间
        String currentTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        app.setInsertdte(currentTime);
        app.setUpdatedte(currentTime);
        System.out.println(app.getInsertdte());
        System.out.println(app.getUpdatedte());


        app.setSyokbn("10");
        app.setAppsts("01");

        App app1 = appService.selectByCstId(cstid);
        if (app1 != null) {
            app1 = app;
            appService.updateApp(app1);
            return "apply/A1A01WA01A04 入会申込情報入力";
        } else if (appService.addApp(app)) {

            return "apply/A1A01WA01A04 入会申込情報入力";

        }
//        if (app.getCstid() != null) {
//            appService.updateApp(app);
//            return "apply/A1A01WA01A04 入会申込情報入力";
//        } else


        return "success";
    }
    @RequestMapping("xinxi1b")
    public String xinxi1b(App app, Model model, HttpSession session) throws SQLException {


        String cstid = (String) session.getAttribute("cstid");
        app.setCstid(cstid);

        // 设置日期字段为当前时间
        String currentTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        app.setInsertdte(currentTime);
        app.setUpdatedte(currentTime);
        System.out.println(app.getInsertdte());
        System.out.println(app.getUpdatedte());


        app.setSyokbn("10");
        app.setAppsts("01");

        App app1 = appService.selectByCstId(cstid);
        if (app1 != null) {
            app1 = app;
            appService.updateApp(app1);
            return "apply/b";
        } else if (appService.addApp(app)) {

            return "apply/b";

        }
//        if (app.getCstid() != null) {
//            appService.updateApp(app);
//            return "apply/A1A01WA01A04 入会申込情報入力";
//        } else


        return "error";
    }
    @RequestMapping("b")
    public String b(@Valid  App app,BindingResult bindingResult, Model model, HttpSession session) throws SQLException {

        // 检查是否有验证错误
        if (bindingResult.hasErrors()) {
            model.addAttribute("app", app);
            return "apply/b"; // 返回表单页面显示错误
        }
        String cstid = (String) session.getAttribute("cstid");
        app.setCstid(cstid);

        // 设置日期字段为当前时间
        String currentTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        app.setInsertdte(currentTime);
        app.setUpdatedte(currentTime);
        System.out.println(app.getInsertdte());
        System.out.println(app.getUpdatedte());


        app.setSyokbn("10");
        app.setAppsts("01");

        App app1 = appService.selectByCstId(cstid);
        if (app1 != null) {
            app1 = app;
            appService.updateApp(app1);
            return "apply/A1A01WA01A06入会申込情報入力";
        } else if (appService.addApp(app)) {

            return "apply/A1A01WA01A06入会申込情報入力";

        }
        return "error";
    }

    @RequestMapping("xinxi2")
    public String xinxi2(@Valid  App app,BindingResult bindingResult, Model model, HttpSession session) throws SQLException {

        // 检查是否有验证错误
        if (bindingResult.hasErrors()) {
            model.addAttribute("app", app);
            return "apply/A1A01WA01A04 入会申込情報入力"; // 返回表单页面显示错误
        }

        String cstid = (String) session.getAttribute("cstid");
        app.setCstid(cstid);
        System.out.println(app.getCstid());

        App appold = appService.selectByCstId(cstid);

        if (appold != null) {
            DBUtilAppChange.mergeData(app, appold);
            System.out.println(app);
            System.out.println(appold);
            // 设置日期字段为当前时间
            String currentTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            appold.setInsertdte(currentTime);
            appold.setUpdatedte(currentTime);
            appold.setSyokbn("11");
            appold.setAppsts("01");
            appold.setBrdcd("1");
            appold.setCrdshucd("01");
            appold.setKzkkbn("1");
            appold.setSkjsskflg("1");
            appold.setSskcd("1");
            appold.setDeleteflg("1");

            // 检查家族カード案内メール希望是否为"無"(值为1)
            if ("1".equals(app.getKzkkbn())) {
                // 设置标识，表示不需要家族卡片
                session.setAttribute("skipFamilyCard", "true");
            } else {
                session.removeAttribute("skipFamilyCard");
            }

            if (appService.updateApp(appold)) {
                return "apply/A1A01WA01A05 入会申込情報入力";
            }
        }
        return "error";
    }
    @RequestMapping("xinxi2c")
    public String xinxi2c(App app, Model model, HttpSession session) throws SQLException {
        String cstid = (String) session.getAttribute("cstid");
        app.setCstid(cstid);
        System.out.println(app.getCstid());

        App appold = appService.selectByCstId(cstid);

        if (appold != null) {
            DBUtilAppChange.mergeData(app, appold);
            System.out.println(app);
            System.out.println(appold);
            // 设置日期字段为当前时间
            String currentTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            appold.setInsertdte(currentTime);
            appold.setUpdatedte(currentTime);
            appold.setSyokbn("11");
            appold.setAppsts("01");
            appold.setBrdcd("1");
            appold.setCrdshucd("01");
            appold.setKzkkbn("1");
            appold.setSkjsskflg("1");
            appold.setSskcd("1");
            appold.setDeleteflg("1");

            // 检查家族カード案内メール希望是否为"無"(值为1)
            if ("1".equals(app.getKzkkbn())) {
                // 设置标识，表示不需要家族卡片
                session.setAttribute("skipFamilyCard", "true");
            } else {
                session.removeAttribute("skipFamilyCard");
            }

            if (appService.updateApp(appold)) {
                return "apply/c";
            }
        }
        return "error";
    }
    @RequestMapping("c")
    public String c(@Valid  App app,BindingResult bindingResult, Model model, HttpSession session) throws SQLException {
        // 检查是否有验证错误
        if (bindingResult.hasErrors()) {
            model.addAttribute("app", app);
            return "apply/c"; // 返回表单页面显示错误
        }

        String cstid = (String) session.getAttribute("cstid");
        app.setCstid(cstid);
        System.out.println(app.getCstid());

        App appold = appService.selectByCstId(cstid);

        if (appold != null) {
            DBUtilAppChange.mergeData(app, appold);
            System.out.println(app);
            System.out.println(appold);
            // 设置日期字段为当前时间
            String currentTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            appold.setInsertdte(currentTime);
            appold.setUpdatedte(currentTime);
            appold.setSyokbn("11");
            appold.setAppsts("01");
            appold.setBrdcd("1");
            appold.setCrdshucd("01");
            appold.setKzkkbn("1");
            appold.setSkjsskflg("1");
            appold.setSskcd("1");
            appold.setDeleteflg("1");

            // 检查家族カード案内メール希望是否为"無"(值为1)
            if ("1".equals(app.getKzkkbn())) {
                // 设置标识，表示不需要家族卡片
                session.setAttribute("skipFamilyCard", "true");
            } else {
                session.removeAttribute("skipFamilyCard");
            }

            if (appService.updateApp(appold)) {
                return "apply/A1A01WA01A06入会申込情報入力";
            }
        }
        return "error";
    }
    @RequestMapping("xinxi3")
    public String xinxi3(@Valid App app,BindingResult bindingResult, Model model, HttpSession session) throws SQLException {

        if (bindingResult.hasErrors()) {
            model.addAttribute("app", app);
            return "apply/A1A01WA01A05 入会申込情報入力";
        }
        return "apply/A1A01WA01A06入会申込情報入力";
    }


    @RequestMapping("biangeng")
    public String biangeng(App app, Model model, HttpSession session) throws SQLException {

        String cstid = (String) session.getAttribute("cstid");
        app.setCstid(cstid);
        System.out.println(app.getCstid());

        App appold = appService.selectByCstId(cstid);

        DBUtilAppChange.mergeData(app, appold);
        System.out.println(app);
        System.out.println(appold);
        // 设置日期字段为当前时间
        String currentTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        appold.setInsertdte(currentTime);
        appold.setUpdatedte(currentTime);
        appold.setSyokbn("13");
        appold.setAppsts("01");

        appold.setBrdcd("1");
        appold.setCrdshucd("01");
        appold.setKzkkbn("1");
        appold.setSkjsskflg("1");
        appold.setSskcd("1");
        appold.setDeleteflg("1");
        if (appService.updateApp(appold)) {
            return "apply/A1A01WC01A01_お支払口座登録・変更";
        }
        return "error";
    }

    @RequestMapping("biangeng02")
    public String biangeng02(@Valid App app, BindingResult bindingResult, Model model, HttpSession session) throws SQLException {
        // 检查是否有验证错误
        if (bindingResult.hasErrors()) {
            model.addAttribute("app", app);
            return "apply/A1A01WC01A01_お支払口座登録・変更";
        }
        String cstid = (String) session.getAttribute("cstid");
        app.setCstid(cstid);
        System.out.println(app.getCstid());

        App appold = appService.selectByCstId(cstid);

        DBUtilAppChange.mergeData(app, appold);
        System.out.println(app);
        System.out.println(appold);
        // 设置日期字段为当前时间
        String currentTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        appold.setInsertdte(currentTime);
        appold.setUpdatedte(currentTime);
        appold.setSyokbn("21");
        appold.setAppsts("05");

        appold.setSskcd("1");
        appold.setDeleteflg("1");
        appold.setBrdcd("1");
        appold.setCrdshucd("01");
        appold.setKzkkbn("1");
        appold.setSkjsskflg("1");
        if (appService.updateApp(appold)) {
//        // 检查是否需要跳过家族卡片申请
//        String skipFamilyCard = (String) session.getAttribute("skipFamilyCard");
//        if ("true".equals(skipFamilyCard)) {
//            // 直接跳转到确认书类上传页面
//            return "apply/A1A01WD01A01_本人・家族確認書類アップロード";
//        } else {
//            // 按照原来的流程
            return "apply/A1A01WC01A11_お支払口座確認";
//        }
        }
        return "error";
    }


    @RequestMapping("tofamily1")
    public String tofamily1(App app, Model model, HttpSession session) throws SQLException {
        // 检查是否需要跳过家族卡片申请
        String skipFamilyCard = (String) session.getAttribute("skipFamilyCard");
        if ("true".equals(skipFamilyCard)) {
            // 直接跳转到确认书类上传页面
            return "apply/A1A01WD01A01_本人・家族確認書類アップロード";
        } else {
            // 按照原来的流程

            return "apply/A1A01WB01A01_家族カード申込情報入力";
        }
    }


    @RequestMapping("family1")
    public String family1(@Valid App app, BindingResult bindingResult, Model model, HttpSession session) throws SQLException {
// 检查是否有验证错误
        if (bindingResult.hasErrors()) {
            model.addAttribute("app", app);
            return "apply/A1A01WB01A01_家族カード申込情報入力";
        }
        String cstid = (String) session.getAttribute("cstid");
        app.setCstid(cstid);
        System.out.println(app.getCstid());

        App appold = appService.selectByCstId(cstid);

        DBUtilAppChange.mergeData(app, appold);
        System.out.println(app);
        System.out.println(appold);
        // 设置日期字段为当前时间
        String currentTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        appold.setInsertdte(currentTime);
        appold.setUpdatedte(currentTime);
        appold.setSyokbn("11");
        appold.setAppsts("10");
        appold.setSskcd("1");
        appold.setDeleteflg("1");
        appold.setBrdcd("1");
        appold.setCrdshucd("01");
        appold.setKzkkbn("1");
        appold.setSkjsskflg("1");

        if (appService.updateApp(appold)) {
            return "apply/A1A01WB01A11_家族カード申込情報確認";
        }
        return "error";
    }


    @RequestMapping("tofamily2")
    public String tofamily2(App app, Model model, HttpSession session) throws SQLException {


        return "apply/A1A01WD01A01_本人・家族確認書類アップロード";
    }


    @RequestMapping("benren")
    public String benren(App app, Model model, HttpSession session) throws SQLException {

        String cstid = (String) session.getAttribute("cstid");
        app.setCstid(cstid);
        System.out.println(app.getCstid());

        App appold = appService.selectByCstId(cstid);

        DBUtilAppChange.mergeData(app, appold);
        System.out.println(app);
        System.out.println(appold);
        // 设置日期字段为当前时间
        String currentTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        appold.setInsertdte(currentTime);
        appold.setUpdatedte(currentTime);
        appold.setSyokbn("11");
        appold.setAppsts("10");
        appold.setSskcd("1");
        appold.setDeleteflg("1");
        appold.setBrdcd("1");
        appold.setCrdshucd("01");
        appold.setKzkkbn("1");
        appold.setSkjsskflg("1");
        if (appService.updateApp(appold)) {
            return "redirect:/tologin?registerSuccess=true";
        }
        return "error";
    }
}

